package com.ququ.syb.business.account.service;

import com.ququ.common.result.Result;
import com.ququ.common.result.ResultJson;
import com.ququ.common.utils.MD5Util;
import com.ququ.common.utils.RegExpUtil;
import com.ququ.syb.business.account.fegin.AccountFegin;
import com.ququ.syb.common.constant.RedisKeyConstant;
import com.ququ.syb.common.pojo.AccountInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ququ.syb.business.account.redis.RedisDao;
import redis.clients.jedis.Jedis;

import java.util.List;

@Service
public class AccountBusinessService {

    private Logger logger = LoggerFactory.getLogger(AccountBusinessService.class);

    @Autowired
    private AccountFegin accountFegin;
    @Autowired
    private RedisDao redisDao;
    @Autowired
    private BlackListService blackListService;

    //注册生意宝账户
    public Result<AccountInfo> register(String accountid,
                                        String name,
                                        String mobile,
                                        String accountname,
                                        String appcode,
                                        String partner,
                                        String idcardno,
                                        String password){
        Jedis jedis = redisDao.getJedis();
        try {
            if (StringUtils.isBlank(mobile) || !(RegExpUtil.isMobile(mobile) || mobile.startsWith("100")))
                return new Result<>(-1, "注册失败，手机号为空或非法");
            if (StringUtils.isBlank(appcode))
                return new Result<>(-1, "注册失败，缺少应用代码");
            if (StringUtils.isBlank(accountname))
                accountname = name;
            if (blackListService.isBlackListMobile(mobile))
                return new Result<>(-1, "注册失败，手机号是黑名单用户");
            if (StringUtils.isNotBlank(idcardno) && blackListService.isBlackListIdcard(idcardno))
                return new Result<>(-1, "注册失败，身份证号是黑名单用户");
            //生成token
            accountid = StringUtils.isBlank(accountid) ? jedis.incr(RedisKeyConstant.LAST_ACCOUNT_ID).toString() : accountid;
            Result<AccountInfo> result = accountFegin.getAccountInfo(accountid);
            if (result.getStatus() == 1)
                return result;
            String token = MD5Util.md5(accountid + System.currentTimeMillis()).substring(0, 32).toLowerCase();
            if (StringUtils.isBlank(password))
                password = MD5Util.md5(StringUtils.right(mobile, 6)).toLowerCase();
            Integer allowpay = 1;
            Integer allowwithdraw = 1;
            result = accountFegin.saveAccountInfo(accountid, name, mobile, accountname, appcode, partner, idcardno, allowpay, allowwithdraw, token, password);
            logger.debug("注册结果:{}", result);
            return result;
        }catch (Exception e){
            logger.error("生意宝账户注册异常",e);
            return new Result<>(-1,"生意宝注册账户异常:"+e.getMessage());
        }finally {
            redisDao.close(jedis);
        }
    }

    //查询生意宝账户
    public Result<AccountInfo> getAccountInfo(String accountid){
        return accountFegin.getAccountInfo(accountid);
    }

    //获取token
    public Result<String> getToken(String accountid){
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new Result<>(-1,"无此生意宝账户"+accountid);
        else
            return new Result<>(accountInfo.getToken());
    }

    //获取交易密码
    public Result<String> getPassword(String accountid){
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new Result<>(-1,"无此生意宝账户"+accountid);
        else
            return new Result<>(accountInfo.getPassword().substring(8,24));
    }


    //检查交易密码
    public Result<Integer> checkPassword(String accountid,String password){
        if (password.length() != 32)
            return new Result<>(-1,"密码长度不正确");
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new Result<>(-1,"无此生意宝账户"+accountid);
        else
            return new Result<>(accountInfo.getPassword().substring(8,24).equals(password.substring(8,24).toLowerCase())?1:0);
    }

    //重置交易密码
    public ResultJson setPassword(String accountid,String password){
        if (password.length() != 32)
            return new ResultJson(-1,"密码长度不正确");
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new ResultJson(-1,"无此生意宝账户"+accountid);
        else {
            accountInfo.setPassword(password.toLowerCase());
            accountFegin.updateAccountInfo(accountInfo.getId(),
                    accountid,
                    null,null,null,null,null,null,null,null,null,
                    password,
                    null);
            return new ResultJson();
        }
    }

    //修改生意宝账户
    public Result<AccountInfo> update(
                              String accountid,
                              String name,
                              String mobile,
                              String accountname,
                              String appcode,
                              String partner,
                              String idcardno,
                              Integer allowpay,
                              Integer allowwithdraw,
                              String token,
                              String password,
                              Integer status){
        if (StringUtils.isNotBlank(mobile) && blackListService.isBlackListMobile(mobile))
            return new Result<>(-1, "更新失败，手机号是黑名单用户");
        if (StringUtils.isNotBlank(idcardno) && blackListService.isBlackListIdcard(idcardno))
            return new Result<>(-1, "更新失败，身份证号是黑名单用户");
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new Result<>(-1,"无此生意宝账户"+accountid);
        else {
            if (blackListService.isBlackListMobile(accountInfo.getMobile()))
                return new Result<>(-1, "更新失败，手机号是黑名单用户");
            if (StringUtils.isNotBlank(accountInfo.getIdcardno()) && blackListService.isBlackListIdcard(accountInfo.getIdcardno()))
                return new Result<>(-1, "更新失败，身份证号是黑名单用户");
            return accountFegin.updateAccountInfo(accountInfo.getId(), accountid, name, mobile, accountname, appcode, partner, idcardno, allowpay, allowwithdraw, token, password, status);
        }
    }

    //冻结生意宝账户
    public ResultJson freezeAccount(String accountid) {
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new ResultJson(-1, "无此生意宝账户" + accountid);
        else {
            accountFegin.updateAccountInfo(accountInfo.getId(),
                    accountid,
                    null, null, null, null, null, null, null, null, null,
                    null,
                    0);
            return new ResultJson();
        }
    }

    //解冻生意宝账户
    public ResultJson unfreezeAccount(String accountid) {
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new ResultJson(-1, "无此生意宝账户" + accountid);
        else {
            accountFegin.updateAccountInfo(accountInfo.getId(),
                    accountid,
                    null, null, null, null, null, null, null, null, null,
                    null,
                    1);
            return new ResultJson();
        }
    }


    //检查是否冻结
    public Result<Integer> checkAccountFreezeStatus(String accountid) {
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new Result<>(-1, "无此生意宝账户" + accountid);
        else {
            return new Result<>(accountInfo.getStatus()==0?1:0);
        }
    }

    //列出所有冻结账户
    public Result<List<AccountInfo>> listFreezeAccountInfo(){
        return accountFegin.listAccountInfo(null,null,0);
    }

    //修改允许支付状态
    public Result<AccountInfo> setAllowPay(String accountid,Integer allowpay){
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new Result<>(-1, "无此生意宝账户" + accountid);
        else {
            accountFegin.updateAccountInfo(accountInfo.getId(),
                    accountid,
                    null, null, null, null, null, null, allowpay,
                    null, null,null,null);
            return accountFegin.getAccountInfo(accountid);
        }
    }

    //检查是否允许支付
    public Result<Integer> getAllowPay(String accountid) {
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new Result<>(-1, "无此生意宝账户" + accountid);
        else
            return new Result<>(accountInfo.getAllowpay());
    }

    //修改允许提现状态
    public Result<AccountInfo> setAllowWithdraw(String accountid,Integer allowwithdraw){
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new Result<>(-1, "无此生意宝账户" + accountid);
        else {
            accountFegin.updateAccountInfo(accountInfo.getId(),
                    accountid,
                    null, null, null, null, null, null, null,
                    allowwithdraw, null,null,null);
            return accountFegin.getAccountInfo(accountid);
        }
    }

    //检查是否允许提现
    public Result<Integer> getAllowWithdraw(String accountid) {
        AccountInfo accountInfo = accountFegin.getAccountInfo(accountid).getData();
        if (accountInfo == null)
            return new Result<>(-1, "无此生意宝账户" + accountid);
        else
            return new Result<>(accountInfo.getAllowwithdraw());
    }


}
